/**
 * Created by oureda on 4/23/16.
 */
var server = require("../index").server;
var app = require("../index").app;
var io = require("socket.io")(server);

var Hook = require("../lib/hook");
var Task = require("../task/index");
var dataTask = require("../task/data");

var iots = [];
var sockets = [];
app.locals.iots = iots;
app.locals.sockets = sockets;

var phoneSocket = {};
var isEmptyObject = function (obj) {
    var i;
    for(i in obj){
        return false;
    }
    return true;
};

var listenOnBody = function(state){
    Hook.doActions(sockets,state);           // do hooks
    dataTask.setBodyState(state,1);
    for(s of sockets){
        if(s.name === "phone"){
            s.emit("phoneBody",{
                value : state.value
            });
        }
    }
};

var listenInit = (function(){
    Hook.addAction(listenOnBody,Task.checkPlan,10);      // add checkPlan to hook
}());


io.on("connection",function(socket){
    sockets.push(socket);

    console.log("new connect of iot");

    /**
     * 监听手机的消息
     */
    socket.on("phone",function(data){
        console.log(data);
        if(isEmptyObject(phoneSocket)){
            return;
        }
    });

    /**
     * 监听设备的指令
     */
    socket.on("directive",function(data){
        console.log(data);
        if(isEmptyObject(phoneSocket)){
            return;
        }
        if(data.equipment == "intelDoorBell"){      // 门铃
            phoneSocket.emit("phoneDoorBell",{
                value : data.value
            });
        }else if (data.equipment == "intelBody"){       // 人体感应
            phoneSocket.emit("phoneBody",{
                value : data.value
            });
        }
    });

    /**
     * 手机端或者intel板子登录
     */
    socket.on("login",function(data){
        console.log(data);
        socket.name = data.name;
        if(data.name === "phone"){
            phoneSocket = socket;
        }
        socket.emit("login:logined");
    });

    /**
     * 空调数据
     */
    socket.on("intelAir",function(state){
        console.log(state);
        dataTask.setAirState(state,1);
    });

    /**
     * 人体感应数据
     */
    socket.on("intelBody",listenOnBody);

    /**
     * 传感器
     */
    socket.on("intelSensor",function(data){
        console.log(data);
        dataTask.storeSensorData(data,1);
    });

    /**
     * 门铃
     */
    socket.on("intelDoorBell",function(data){
        console.log(data);
        dataTask.storeDoorBellData(data,1);
        var s;
        for(s of sockets){
            if(s.name === "phone"){
                s.emit("phoneDoorBell",{
                    value : data.value
                });
            }
        }
    });

    /**
     * 插线板
     */
    socket.on("intelPatch",function(state){
        console.log(state);
        dataTask.setPatchState(state,1);
        var s;
        for(s of sockets){
            if(s.name === "phone"){
                s.emit("phonePatch",{value:state.value});
            }
        }
    });

    /**
     * 设备下线
     */
    socket.on("stop",function(){
        console.log("disconnect of iot");
        sockets.splice(sockets.indexOf(socket),1);
    });

    /**
     * 设备下线
     */
    socket.on("disconnect",function(){
        console.log("disconnect of iot");
        sockets.splice(sockets.indexOf(socket),1);
    });
});

module.exports = sockets;